package com.SHA1604.tiger.fragment;

import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.SHA1604.tiger.R;
import com.SHA1604.tiger.adapter.RecommandAdapter;
import com.SHA1604.tiger.base.BaseFragment;
import com.SHA1604.tiger.bean.DailyRecommandBean;
import com.SHA1604.tiger.bean.RecomandTitleBean;
import com.SHA1604.tiger.constant.UrlString;
import com.SHA1604.tiger.utils.OKHttpUtils;
import com.SHA1604.tiger.view.RecomandTitleView;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;


public class HomeFragment extends BaseFragment{
    private ListView mListView;
    private List<DailyRecommandBean.DataBean> mList;
    private DailyRecommandBean mDailyRecommandBean;
    private RecomandTitleView mRecomandTitleView;
    private Handler handler = new Handler();
    @Override
    protected View loadXml(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_home,container,false);
    }


    @Override
    protected void initView(View view) {
        mList = new ArrayList<>();
        mListView = (ListView) view.findViewById(R.id.lv_recommend_daily);
        mRecomandTitleView = new RecomandTitleView(mActivity);
    }

    @Override
    protected void initData() {
        initDailyRecomand();
        initRecomandTitleData();
    }

    private void initRecomandTitleData() {
        OKHttpUtils.newInstance(mActivity).
                getAsyncMainData(UrlString.HOME_RECOMMAND_TITLE, new OKHttpUtils.MyCallBack() {
                    @Override
                    public void onFailure(IOException e) {

                    }

                    @Override
                    public void onResponse(String result) {
                        RecomandTitleBean recomandTitleBean
                                = new Gson().fromJson(result, RecomandTitleBean.class);
                        mRecomandTitleView.setData(recomandTitleBean);
                        mListView.addHeaderView(mRecomandTitleView);
                    }
                },null);
    }

    private void initDailyRecomand() {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(UrlString.HOME_RECOMMAND_DAILY)
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String string = response.body().string();
                mDailyRecommandBean = new Gson().fromJson(string, DailyRecommandBean.class);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        mList.addAll(mDailyRecommandBean.getData());
                        RecommandAdapter adapter = new RecommandAdapter(mActivity,mList);
                        adapter.notifyDataSetChanged();
                        mListView.setAdapter(adapter);
                    }
                });
            }
        });
    }


}
